<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理 -->
      <el-row class="btn_container">
        <el-button size="mini" type="primary" @click="showDialog=true">添加角色</el-button>
      </el-row>
      <el-table v-loading="loading" :data="list">
        <el-table-column align="center" label="角色" prop="name">
          <template v-slot="{row}">
            <!-- 两种场景 -->
            <!-- 编辑的状态-未编辑的状态 -->
            <span v-if="!row.isEdit">{{ row.name }}</span>
            <el-input v-else v-model="row.editRow.name" size="mini" placeholder="请输入角色名称" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="启用" prop="state">
          <template v-slot="{ row }">
            <template v-if="!row.isEdit">  {{ row.state === 1 ? "已启用" : row.state === 0 ? "未启用" : "无" }} </template>
            <el-switch v-else v-model="row.editRow.state" size="mini" :active-value="1" :inactive-value="0" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="描述" prop="description">
          <template v-slot="{row}">
            <span v-if="!row.isEdit">{{ row.description }}</span>
            <el-input v-else v-model="row.editRow.description" type="textarea" size="mini" :rows="2" placeholder="请输入角色描述" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="{row}">
            <template v-if="!row.isEdit">
              <el-button size="mini" type="text">分配权限</el-button>
              <el-button size="mini" type="text" @click="btnEidtRow(row)">编辑</el-button>
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="del(row.id)"
              >
                <template #reference>
                  <el-button style="margin-left: 10px;" size="mini" type="text">删除</el-button>
                </template>
              </el-popconfirm>
            </template>
            <template v-else>
              <el-button size="mini" type="primaey" @click="btnEditOK(row)">确定</el-button>
              <el-button size="mini" @click="row.isEdit=false">取消</el-button>
            </template>

          </template>
        </el-table-column>
      </el-table>
      <el-row style="height: 80px;" type="flex" justify="end" align="middle">
        <el-pagination
          layout="prev, pager, next,jumper,total"
          :total="pageParams.total"
          :page-size="pageParams.pagesize"
          :current-page="pageParams.page"
          @current-change="changePage"
        />
      </el-row>
    </div>
    <el-dialog title="新增角色" :visible.sync="showDialog" @close="close">
      <el-form ref="addForm" label-width="120px" :model="addForm" :rules="addRules">
        <el-form-item label="角色" prop="name">
          <el-input v-model.trim="addForm.name" style="width: 80%;" />
        </el-form-item>
        <el-form-item label="启用" prop="state">
          <el-switch
            v-model="addForm.state"
            active-color="#67A6D9"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input
            v-model.trim="addForm.description"
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            style="width: 80%;"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-row style="width: 100%;" type="flex" justify="center">
          <el-button size="mini" type="primary" @click="btnOK">确认</el-button>
          <el-button size="mini" @click="close">取消</el-button>
        </el-row>
      </template>

    </el-dialog>
  </div>
</template>
<script>
import { addRoleAPI, delRoleAPI, getRoleListAPI, updateRoleAPI } from '@/api'

export default {
  name: 'Role',
  data() {
    return {
      list: [],
      loading: false,
      pageParams: {
        page: 1, // 第几页
        pagesize: 5, // 每页几条
        total: 0
      },
      showDialog: false,
      addForm: {
        name: '',
        description: '',
        state: 1
      },
      addRules: {
        name: [
          { required: true, message: '角色姓名不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      this.loading = true
      const { rows, total } = await getRoleListAPI(this.pageParams)
      this.list = rows
      // 针对每一行数据添加一个编辑标记
      this.list.forEach(item => {
        // item.isEdit = false // 添加一个属性 初始值为false
        // 数据响应式的问题  数据变化 视图更新
        // 添加的动态属性 不具备响应式特点
        // this.$set(目标对象, 属性名称, 初始值) 可以针对目标对象 添加的属性 添加响应式
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
      this.pageParams.total = total
      this.loading = false
    },
    changePage(newPage) {
      this.pageParams.page = newPage // 赋值当前页码
      this.getRoleList()
    },
    async btnOK() {
      await this.$refs.addForm.validate()
      await addRoleAPI(this.addForm)
      this.$message.success('新增角色成功')
      this.getRoleList()
      this.close()
    },
    close() {
      this.$refs.addForm.resetFields()
      this.showDialog = false
      // this.getRoleList()
    },
    async del(id) {
      await delRoleAPI(id)
      if (this.list.length === 1) this.pageParams.page--
      this.getRoleList()
    },
    btnEidtRow(row) {
      row.isEdit = true // 改变行的编辑状态
      row.editRow.name = row.name
      row.editRow.state = row.state
      row.editRow.description = row.description
    },
    async btnEditOK(row) {
      if (row.editRow.name && row.editRow.description) {
        await updateRoleAPI({
          id: row.id,
          ...row.editRow
        })
        this.$message.success('角色更新成功')

        Object.assign(row, { ...row.editRow, isEdit: false })
      } else {
        this.$message.warning('角色名称和描述不能为空')
      }
    }
  }

}
</script>
<style scoped>
.btn_container{
  padding: 10px 20px;
}
</style>
